package com.summba.analysis.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.summba.common.Common;

public class WordUtil {
	private Map<String,Integer> wordMapper = new HashMap<String, Integer>();
	
	public WordUtil(){
		
	}
	
	public void addCorpus(String corpus){
		String[] wordArray = corpus.split(" ");
		for (int i = 0; i < wordArray.length; i++) {
			if( wordMapper.containsKey(wordArray[i]) ){
				Integer iTemp = wordMapper.get(wordArray[i]);
				iTemp++;
				wordMapper.put(wordArray[i], iTemp++);
			}else{
				wordMapper.put(wordArray[i], 1);
			}
		}
	}
	
	public void showResult(){
		List<Map.Entry<String, Integer>> infoIds = 
			new ArrayList< Map.Entry<String, Integer> >(wordMapper.entrySet());
		
		Collections.sort(infoIds, 
				new Comparator<Map.Entry<String, Integer>>() { 
					public int compare(Map.Entry<String, Integer> o1, 
					Map.Entry<String, Integer> o2) { 
						return (o2.getValue() - o1.getValue()); 
					} 
				});
		for (int i = 0; i < infoIds.size(); i++) { 
			String id = infoIds.get(i).toString();
			//Common.appendFile(resultFile, id+ "\n");
			System.out.println(id); 
	}
		
	}

}
